package com.cgq.pro.action;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.cgq.base.action.BaseAction;
import com.cgq.pro.entity.Ecity;
import com.cgq.pro.entity.Permission;
import com.cgq.pro.entity.PhotoType;
import com.cgq.pro.entity.Region;
import com.cgq.pro.entity.User;
import com.cgq.pro.service.EcityService;
import com.cgq.pro.service.PermissionService;
import com.cgq.pro.service.PhotoTypeService;
import com.cgq.pro.service.RegionService;
import com.cgq.pro.service.UserService;
import com.cgq.utils.Constants;
import com.cgq.utils.IdGenerator;
import com.cgq.utils.MD5;

/**
 * 初始化数据
 * @author chenkaile
 *
 */
@Controller
public class InitAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	@Autowired
	private PermissionService permissionService;
	@Autowired
	private UserService userService;
	@Autowired
	private RegionService regionService;
	@Autowired
	private PhotoTypeService photoTypeService;
	@Autowired
	private EcityService ecityService;

	@Override
	public String execute() throws Exception {
		
		//初始化权限数据
		Permission permission=new Permission();
		String perId=IdGenerator.getId(service, Constants.TABLE_PERMISSION, Constants.PREFIX_PERMISSION, 10);
		permission.setId(perId);
		permission.setName("管理员");
		permission.setPerFlag(1);
		permissionService.save(permission);
		
		permission=new Permission();
		permission.setId(IdGenerator.getId(service, Constants.TABLE_PERMISSION, Constants.PREFIX_PERMISSION, 10));
		permission.setName("普通用户");
		permission.setPerFlag(2);
		permissionService.save(permission);
		
		//初始化行政区
		Region region=new Region();
		region.setId(IdGenerator.getId(service, Constants.TABLE_REGION,
				Constants.PREFIX_REGION, 10));
		region.setName("鼓楼区");
		regionService.save(region);
		
		region=new Region();
		region.setId(IdGenerator.getId(service, Constants.TABLE_REGION,
				Constants.PREFIX_REGION, 10));
		region.setName("云龙区");
		regionService.save(region);
		
		region=new Region();
		region.setId(IdGenerator.getId(service, Constants.TABLE_REGION,
				Constants.PREFIX_REGION, 10));
		region.setName("泉山区");
		regionService.save(region);
		
		region=new Region();
		region.setId(IdGenerator.getId(service, Constants.TABLE_REGION,
				Constants.PREFIX_REGION, 10));
		region.setName("贾汪区");
		regionService.save(region);
		
		region=new Region();
		region.setId(IdGenerator.getId(service, Constants.TABLE_REGION,
				Constants.PREFIX_REGION, 10));
		region.setName("经济开发区");
		regionService.save(region);
		
		region=new Region();
		region.setId(IdGenerator.getId(service, Constants.TABLE_REGION,
				Constants.PREFIX_REGION, 10));
		region.setName("铜山区");
		regionService.save(region);
		
		region=new Region();
		region.setId(IdGenerator.getId(service, Constants.TABLE_REGION,
				Constants.PREFIX_REGION, 10));
		region.setName("新城区");
		regionService.save(region);
		
		//初始化相片类型
		PhotoType photoType=new PhotoType();
		photoType.setId(IdGenerator.getId(service, Constants.TABLE_USER, Constants.PREFIX_PHOTOTYPE, 10));
		photoType.setName("项目效果图");
		photoType.setType(Constants.PHOTOTYPE_CITY); //市区统计
		photoTypeService.save(photoType);
		
		photoType=new PhotoType();
		photoType.setId(IdGenerator.getId(service, Constants.TABLE_USER, Constants.PREFIX_PHOTOTYPE, 10));
		photoType.setName("户型图");
		photoType.setType(Constants.PHOTOTYPE_CITY); //市区统计
		photoTypeService.save(photoType);
		
		photoType=new PhotoType();
		photoType.setId(IdGenerator.getId(service, Constants.TABLE_USER, Constants.PREFIX_PHOTOTYPE, 10));
		photoType.setName("景观图");
		photoType.setType(Constants.PHOTOTYPE_CITY); //市区统计
		photoTypeService.save(photoType);
		
		photoType=new PhotoType();
		photoType.setId(IdGenerator.getId(service, Constants.TABLE_USER, Constants.PREFIX_PHOTOTYPE, 10));
		photoType.setName("单体图");
		photoType.setType(Constants.PHOTOTYPE_CITY); //市区统计
		photoTypeService.save(photoType);
		
		photoType=new PhotoType();
		photoType.setId(IdGenerator.getId(service, Constants.TABLE_USER, Constants.PREFIX_PHOTOTYPE, 10));
		photoType.setName("样板间图");
		photoType.setType(Constants.PHOTOTYPE_CITY); //市区统计
		photoTypeService.save(photoType);
		
		photoType=new PhotoType();
		photoType.setId(IdGenerator.getId(service, Constants.TABLE_USER, Constants.PREFIX_PHOTOTYPE, 10));
		photoType.setName("小区实景图");
		photoType.setType(Constants.PHOTOTYPE_CITY); //市区统计
		photoTypeService.save(photoType);
		
		photoType=new PhotoType();
		photoType.setId(IdGenerator.getId(service, Constants.TABLE_USER, Constants.PREFIX_PHOTOTYPE, 10));
		photoType.setName("行政区地图");
		photoType.setType(Constants.PHOTOTYPE_REGION); //行政区统计
		photoTypeService.save(photoType);
		
		photoType=new PhotoType();
		photoType.setId(IdGenerator.getId(service, Constants.TABLE_USER, Constants.PREFIX_PHOTOTYPE, 10));
		photoType.setName("风景图");
		photoType.setType(Constants.PHOTOTYPE_REGION); //行政区统计
		photoTypeService.save(photoType);
		
		photoType=new PhotoType();
		photoType.setId(IdGenerator.getId(service, Constants.TABLE_USER, Constants.PREFIX_PHOTOTYPE, 10));
		photoType.setName("配套设施图");
		photoType.setType(Constants.PHOTOTYPE_REGION); //行政区统计
		photoTypeService.save(photoType);
		
		photoType=new PhotoType();
		photoType.setId(IdGenerator.getId(service, Constants.TABLE_USER, Constants.PREFIX_PHOTOTYPE, 10));
		photoType.setName("规划图");
		photoType.setType(Constants.PHOTOTYPE_REGION); //行政区统计
		photoTypeService.save(photoType);
		
		//初始化用户数据，默认添加一条数据
		User user=new User();
		user.setId(IdGenerator.getId(service, Constants.TABLE_USER, Constants.PREFIX_USER, 10));
		user.setName("admin");
		user.setPassword(MD5.MD5Encode("123456"));
		user.setPerId(permissionService.find(perId));
		userService.save(user);
		
		//初始化城市信息
		ecity=new Ecity(); 
		ecity.setName("北京");
		ecity.setRemark("测试信息");
		ecity.setUserCre(user.getName());
		ecity.setUserUpd(user.getName());
		ecity.setDateCre(new Date());
		ecity.setDateUpd(ecity.getDateCre());
		ecityService.save(ecity);
		
		return "success";
	}
}
